//----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//----------------------------------------------------------------------------------


using System;
using System.IO;
using System.Text;

public class Task
{
    public string TaskId { get; set; }
    public string Subject { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime DueDate { get; set; }
    public string Status { get; set; }
    public string Priority { get; set; }
    public bool IsComplete { get; set; }
    public int PercentComplete { get; set; }
    public string BlobId { get; set; }

    public Stream CreateBlobStream()
    {
        MemoryStream ms = new MemoryStream();
        StreamWriter sw = new StreamWriter(ms);
        
        sw.Write(CreateTask(this.StartDate, this.DueDate, this.Subject, null));
        sw.Flush();
        ms.Position = 0; //reset

        //set the blob id
        this.BlobId = Guid.NewGuid().ToString();

        return ms;
    }

    private string CreateTask(DateTime start, DateTime end, string sub, string msgBody)
    {
        StringBuilder sbvCalendar = new StringBuilder();

        //Header
        sbvCalendar.Append("METHOD: REQUEST");
        sbvCalendar.Append("\n");
        sbvCalendar.Append("BEGIN:VCALENDAR");
        sbvCalendar.Append("\n");
        sbvCalendar.Append("PRODID:-//Microsoft Corporation//Outlook ");
        sbvCalendar.Append("\n");
        sbvCalendar.Append("MIMEDIR//ENVERSION:1.0");
        sbvCalendar.Append("\n");
        sbvCalendar.Append("BEGIN:VEVENT");
        sbvCalendar.Append("\n");

        //DTSTART 
        sbvCalendar.Append("DTSTART:");
        string hour = start.Hour.ToString();
        if (hour.Length < 2) { hour = "0" + hour; }

        string min = start.Minute.ToString();
        if (min.Length < 2) { min = "0" + min; }

        string sec = start.Second.ToString();
        if (sec.Length < 2) { sec = "0" + sec; }

        string mon = start.Month.ToString();
        if (mon.Length < 2) { mon = "0" + mon; }

        string day = start.Day.ToString();
        if (day.Length < 2) { day = "0" + day; }

        sbvCalendar.Append(start.Year.ToString() + mon + day
                               + "T" + hour + min + sec);
        sbvCalendar.Append("\n");

        //DTEND
        sbvCalendar.Append("DTEND:");
        hour = end.Hour.ToString();
        if (hour.Length < 2) { hour = "0" + hour; }

        min = end.Minute.ToString();
        if (min.Length < 2) { min = "0" + min; }

        sec = end.Second.ToString();
        if (sec.Length < 2) { sec = "0" + sec; }

        mon = end.Month.ToString();
        if (mon.Length < 2) { mon = "0" + mon; }

        day = end.Day.ToString();
        if (day.Length < 2) { day = "0" + day; }

        sbvCalendar.Append(end.Year.ToString() + mon +
                     day + "T" + hour + min + sec);
        sbvCalendar.Append("\n");

        //Location
        sbvCalendar.Append("LOCATION;ENCODING=QUOTED-PRINTABLE: "
                                                 + String.Empty);
        sbvCalendar.Append("\n");

        //Message body
        sbvCalendar.Append("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:"
                                                        + msgBody);
        sbvCalendar.Append("\n");

        //Subject
        sbvCalendar.Append("SUMMARY;ENCODING=QUOTED-PRINTABLE:"
                                                        + sub);
        sbvCalendar.Append("\n");

        //Priority
        sbvCalendar.Append("PRIORITY:3");
        sbvCalendar.Append("\n");
        sbvCalendar.Append("END:VEVENT");
        sbvCalendar.Append("\n");
        sbvCalendar.Append("END:VCALENDAR");
        sbvCalendar.Append("\n");

        return sbvCalendar.ToString();
    }

}
